<template>
  <div>
    <!--   TODO：后续再说 -->
    <el-button type="primary" link>详情</el-button>
    <el-dropdown v-if="buttons?.length">
      <span class="el-dropdown-link">展开</span>
      <template #dropdown>
        <el-dropdown-menu>
          <el-dropdown-item v-for="item in buttons" @click="() => item.fn(rowData)">
            {{ item.label }}
          </el-dropdown-item>
        </el-dropdown-menu>
      </template>
    </el-dropdown>
  </div>
</template>

<script setup>
import { computed, getCurrentInstance } from "vue";
import { useRouter } from "vue-router";
import { resetClientInfo } from "@/apis/engineeringManage";

const props = defineProps({
  rowData: {
    type: Object,
    default: () => ({})
  }
});
const { proxy } = getCurrentInstance();
const router = useRouter();

const commonOperates = {
  assign: {
    label: "分配",
    fn: data => handelDistribution(data)
  },
  confirmStart: {
    label: "开工确认",
    fn: data => handelWorkStartConfirm(data)
  },
  resetTask: {
    label: "重置任务",
    fn: data => handelResetTask(data)
  },
  initiateChange: {
    label: "发起增减项",
    fn: data => {
      console.log("发起增减项", data);
    }
  },
  adjustPlan: {
    label: "调整计划",
    fn: data => {
      console.log("调整计划", data);
    }
  },
  initiateRectification: {
    label: "发起整改",
    fn: data => {
      console.log("发起整改", data);
    }
  },
  applyShutdown: {
    label: "停工申请",
    fn: data => {
      console.log("停工申请", data);
    }
  },
  resumeWork: {
    label: "复工",
    fn: data => {
      console.log("复工", data);
    }
  },
  cancelShutdown: {
    label: "取消停工申请",
    fn: data => {
      console.log("取消停工申请", data);
    }
  }
  // applyFinalSettlement: {
  //   label: "申请决算",
  //   fn: data => {
  //     console.log("申请决算", data);
  //   }
  // }
};
const constructionStatus = new Map([
  [
    "not_start",
    {
      label: "未开始",
      operates: [commonOperates.assign, commonOperates.confirmStart, commonOperates.resetTask, commonOperates.initiateChange]
    }
  ],
  [
    "ing",
    {
      label: "进行中",
      operates: [
        commonOperates.assign,
        commonOperates.adjustPlan,
        commonOperates.resetTask,
        commonOperates.initiateRectification,
        commonOperates.initiateChange,
        commonOperates.applyShutdown
      ]
    }
  ],
  [
    "shutdown",
    {
      label: "已停工",
      operates: [commonOperates.assign, commonOperates.resumeWork]
    }
  ],
  [
    "shutdown_ing",
    {
      label: "停工审核中",
      operates: [
        commonOperates.assign,
        commonOperates.adjustPlan,
        commonOperates.resetTask,
        commonOperates.initiateRectification,
        commonOperates.initiateChange,
        commonOperates.cancelShutdown
      ]
    }
  ],
  [
    "complete",
    // commonOperates.applyFinalSettlement,
    {
      label: "已交付",
      operates: [commonOperates.initiateRectification, commonOperates.initiateChange]
    }
  ],
  [
    "time_out",
    {
      label: "已超期",
      operates: []
    }
  ],
  [
    "delay_risk",
    {
      label: "延期风险",
      operates: []
    }
  ]
]);

/**
 * 分配
 * @param row
 */
const handelDistribution = row => {
  router.push({
    path: "/distribution",
    query: {
      customerId: row.customerId,
      reformType: false,
      stageType: 2
    }
  });
};
/**
 * 开工确认
 */
const handelWorkStartConfirm = row => {
  console.log("row", row);
};

/**
 * 重置任务
 * @param data
 */
const handelResetTask = data => {
  proxy.$modal
    .confirm("除该客户已完成的任务外，其他任务将按最新流程模板重发，是否确认？", "确认操作？")
    .then(async () => {
      try {
        // isLoadingTable.value = true;
        await resetClientInfo(data.customerId);
        // isLoadingTable.value = false;
        proxy.$modal.msgSuccess("重置成功");
      } catch (err) {
        // isLoadingTable.value = false;
      }
    })
    .catch(() => {});
};

/**
 * 开工
 */

/**
 * 停工
 */

const buttons = computed(() => {
  // props.rowData.state
  const data = "not_start";
  return constructionStatus.has(data) ? constructionStatus.get(data)?.operates : [];
});
</script>

<style scoped lang="scss">
@import "optionButton";
</style>
